home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / mscheap2 / stdmsc.c < prev    next >
C/C++ Source or Header  |  1990-03-13  |  2KB  |  92 lines

  1. //
  2. //     Copyright (c) 1990 by Optimal Software, All Rights Reserved
  3. //
  4.  
  5. #include <malloc.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. static void check4( char *name, void *(*func)( void *ptr, size_t nbytes ) );
  10. static void report( char *name, void *old, size_t size, void *new );
  11.  
  12. #define PNAME(ptr) ( (ptr) == NULL ? "NULL   " : "pointer" )
  13.  
  14. //
  15. //    Demonstrate the actual behavior of the MSC heap manager
  16. //
  17. void main()
  18. {
  19. size_t size = 0xFFFF;
  20.  
  21. void *malloc0 = malloc(0);
  22.  
  23. #if defined( M_I86SM ) || defined( M_I86MM )
  24.  
  25.    printf("Statistics on \"near\" heap are uninteresting\n");
  26.  
  27.    exit(0);
  28.  
  29. #endif
  30.  
  31. printf("\nOriginal MSC heap manager\n\n");
  32.  
  33. //
  34. //    To change the value returned by _msize(NULL) change the size
  35. //    parameter in the following call to _expand().
  36. //
  37. _expand( NULL, 1234 );    /* set value of _msize(NULL) */
  38.  
  39. printf("  _msize( NULL ) returns %u\n\n", _msize(NULL) );
  40.  
  41. printf("  malloc( 0 ) returns %s to %u bytes\n\n", PNAME(malloc0), _msize(malloc0) );
  42.  
  43. check4( "_expand", _expand );
  44.  
  45. check4( "realloc", realloc );
  46.  
  47. while( size > 0
  48.    &&  malloc( size ) == NULL )
  49.  
  50.    size--;
  51.  
  52. printf("  Allocation limit is %u bytes per block\n\n", size );
  53.  
  54. exit( !(_heapchk() == _HEAPOK) );
  55. }
  56.  
  57. static void check4( char *name, void *(*func)( void *ptr, size_t nbytes ) )
  58. {
  59. void *ptr1 = malloc( 64);
  60. void *ptr2 = malloc(256);
  61.  
  62. void *test00 = (*func)( NULL,   0 );
  63. void *test01 = (*func)( NULL, 128 );
  64. void *test10 = (*func)( ptr1,   0 );
  65. void *test11 = (*func)( ptr2, 128 );
  66.  
  67. report( name, NULL,   0, test00 );
  68. report( name, NULL, 128, test01 );
  69. report( name, ptr1,   0, test10 );
  70. report( name, ptr2, 128, test11 );
  71.  
  72. printf("\n");
  73. }
  74.  
  75. static void report( char *name, void *old, size_t size, void *new )
  76. {
  77. printf( "  %s( %s, %3d ) returns %s",
  78.         name,
  79.         old == NULL ? " NULL" : "!NULL",
  80.         size,
  81.         PNAME( new ) );
  82.  
  83. if (new != NULL)
  84.  
  85.    printf(" to %3u bytes", _msize( new ) );
  86.  
  87. printf("\n");
  88. }
  89.  
  90.  
  91.  
  92.